
import star from '../../assets/star.png'
import Phaser from 'phaser'
import dude from '../../assets/dude.png'

export default class ExtendsOfficialScene extends Phaser.Scene {

    constructor() {
        super('extendsOfficialScene')
    }

    // Initialization code here
    init(data) {
        console.log('--init--')
    }

    // Preload assets here
    preload() {
        console.log('--preload--')
        this.load.image('star', star);
        this.load.spritesheet('dude',
            dude,
            { frameWidth: 32, frameHeight: 48 }
        );
    }

    // 添加元素的方法
    // this.add.image(x, y, textureKey)
    // this.add.graphics() 动态绘制矩形、圆形、线条
    // this.add.sprite(x, y, textureKey) 添加可动画、交互的对象
    // this.add.text(x, y, content, style) 添加文字说明或数值显示
    // Create game objects and set up the scene here 
    create() {
        console.log('--create--')
        let starImg = this.add.image(200, 200, 'star')
        this.twinnsFunDp({"direction":true,"starImg": starImg})
        //创建和播放动画
        this.player = this.physics.add.sprite(100, 100, 'dude')
        this.player.body.setGravityY(100)
        this.player.setCollideWorldBounds(true)
        this.player.setBounce(0.5)
        this.anims.create({
            key: "dude_anim",
            frames: this.anims.generateFrameNumbers('dude', { start: 1, end: 8 }), // 指定使用的帧
            frameRate: 5, // 动画播放速度（帧/秒）
            repeat: -1
        })
        this.player.play('dude_anim')
        this.space = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE)
        this.cursor = this.input.keyboard.createCursorKeys()
        console.log(this.sys)
    }

    //实现一个翻转动画
    twinnsFunDp(params) {
        let {direction,starImg} = params
        this.tweens.add({
            targets: starImg, // 指定要应用补间的目标对象[citation:2]
            x:  Phaser.Math.Between(0, 800), // 将目标对象平移到 x 坐标为 600 的位置[citation:2]
            y: Phaser.Math.Between(0, 600), // 将目标对象平移到 y 坐标为 300 的位置
            alpha: 1, // 将目标对象的透明度调整为 0.5
            duration: 2000, // 补间动画的持续时间为 2000 毫秒[citation:2]
            ease: 'Power1', // 指定补间动画使用的缓动函数类型[citation:2]
            onComplete: () => {
                this.twinnsFunDp({"direction": !params.direction,"starImg": starImg})
            }
        });
    }


    // Update game logic each frame here
    update(time, delta) {
        console.log('--update--')
        // this.player.x=Phaser.Math.Between(0, 800)
        // this.player.y=Phaser.Math.Between(0, 600) 
        if (this.cursor.left.isDown) {
            this.player.x-=10
        } else if (this.cursor.right.isDown) {
            this.player.x+=10
        } else if (this.cursor.up.isDown) {

        } else if (this.cursor.down.isDown) {
            this.player.y +=10
        } else if (this.cursor.space.isDown) {
             this.player.y -=10
        }
    }
}

class SelfSpriter extends Phaser.GameObjects.Sprite {



}

class SelfRect extends Phaser.GameObjects.Rectangle {



}